package com.itly.rwgh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itly.rwgh.pojo.Code;
import com.itly.rwgh.pojo.DataTemp;
import com.itly.rwgh.pojo.Result;
import com.itly.rwgh.service.WeatherIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.itly.rwgh.utils.GetKey.getHfKey;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 罗言
 * @Version: 1.0
 * @Date: 2023-11-21-15:26
 * @Description: 天气指数
 */
@Controller
@RequestMapping("/weatherIndexs")
public class WeatherIndexController {

    @Autowired
    private WeatherIndexService IndexService;

    /**
     * 南明区
     * @return 返回值
     */
    @GetMapping("/getData1")
    @ResponseBody
    public Result getData1() {
        String location = Code.CITY_NMQ_LON + "," + Code.CITY_NMQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------南明区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 1);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(1);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 云岩区
     * @return 返回值
     */
    @GetMapping("/getData2")
    @ResponseBody
    public Result getData2() {
        String location = Code.CITY_YYQ_LON + "," + Code.CITY_YYQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------云岩区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 2);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(2);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 花溪区
     * @return 返回值
     */
    @GetMapping("/getData3")
    @ResponseBody
    public Result getData3() {
        String location = Code.CITY_HXQ_LON + "," + Code.CITY_HXQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------花溪区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 3);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(3);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 乌当区
     * @return 返回值
     */
    @GetMapping("/getData4")
    @ResponseBody
    public Result getData4() {
        String location = Code.CITY_WDQ_LON + "," + Code.CITY_WDQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------乌当区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 4);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(4);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 白云区
     * @return 返回值
     */
    @GetMapping("/getData5")
    @ResponseBody
    public Result getData5() {
        String location = Code.CITY_BYQ_LON + "," + Code.CITY_BYQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------白云区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 5);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(5);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 观山湖区
     * @return 返回值
     */
    @GetMapping("/getData6")
    @ResponseBody
    public Result getData6() {
        String location = Code.CITY_GSHQ_LON + "," + Code.CITY_GSHQ_LAT;
        String get = IndexService.getData(getUrl(location), "GET");
        System.out.println("------------观山湖区----------------");
        if(get.length() > 3) {
            JSONObject jsonObject = JSON.parseObject(get);
            System.out.println("----------实时数据--------" + jsonObject);
            boolean update = IndexService.update(get, 6);
            System.out.println("----------缓存数据 结果: " + update);
            return new Result(jsonObject != null ? Code.GET_OK : Code.GET_ERR, jsonObject);
        }
        DataTemp temp = IndexService.getInfo(6);
        JSONObject jsonObjec2 = JSON.parseObject(temp.getData().toString());
        System.out.println("----------数据库缓存数据--------" + jsonObjec2);
        return new Result(jsonObjec2 != null ? Code.GET_OK : Code.GET_ERR, jsonObjec2);
    }

    /**
     * 封装url
     * @param location 经纬度
     * @return 返回值
     */
    public String getUrl(String location) {
        String type = "0";
        return "https://devapi.qweather.com/v7/indices/1d?location=" + location + "&key=" + getHfKey() + "&type=" + type + "&lang=zh";
    }

}
